package by.grsu.na.dao.impl;

import java.io.Serializable;
import java.util.List;

import by.grsu.na.entity.BlackClient;
import by.grsu.na.table.BlackClientTable;

import com.epam.grsulab.webapi.dao.AbstractDao;


public class BlackClientDao extends AbstractDao<BlackClientTable, BlackClient> {

	public BlackClientDao(final String rootFolderPath) {
		super(rootFolderPath);
	}

	@Override
	public void saveNew(final BlackClient newBlackClient) {

		newBlackClient.setId(getNextId());

		final BlackClientTable blackClientTable = deserializeFromXml();

		blackClientTable.getRows().add(newBlackClient);

		serializeToXml(blackClientTable);
	}

	@Override
	public void update(final BlackClient entity) {

		final BlackClientTable blackClientTable = deserializeFromXml();

		for (final BlackClient row : blackClientTable.getRows()) {
			if (row.getId().equals(entity.getId())) {

				
				row.setClientName(entity.getClientName());
				row.setClientLogin(entity.getClientLogin());
				row.setClientPasword(entity.getClientPasword());
				break;
			}
		}

		serializeToXml(blackClientTable);
	}

	@Override
	public BlackClient get(final Serializable id) {

		final BlackClientTable blackClientTable = deserializeFromXml();

		for (final BlackClient row : blackClientTable.getRows()) {
			if (row.getId().equals(id)) {
				return row;
			}
		}
		return null;
	}

	@Override
	public List<BlackClient> getAll() {

		final BlackClientTable blackClientTable = deserializeFromXml();
		return blackClientTable.getRows();
	}

	@Override
	public void delete(final Serializable id) {

		final BlackClientTable blackClientTable = deserializeFromXml();

		BlackClient toBeDeleted = null;
		for (final BlackClient row : blackClientTable.getRows()) {
			if (row.getId().equals(id)) {

				toBeDeleted = row;
				break;
			}
		}

		blackClientTable.getRows().remove(toBeDeleted);

		serializeToXml(blackClientTable);
	}

	@Override
	protected Class<BlackClientTable> getTableClass() {
		return BlackClientTable.class;
	}

}
